package com.dhcc.bpm.common.utils;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.dhcc.bpm.common.config.TemplateConfig;
import com.dhcc.bpm.modules.api.entity.WsdlElement;
import com.dhcc.bpm.modules.api.entity.WsdlService;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WsdlUtils {
    public static List<WsdlService> createWsdlfromExcel(String excelPath,String address,String saveFilePath,String type,String id,String serviceCode) throws Exception{
        ImportParams importParams = new ImportParams();
        // 数据处理
        importParams.setHeadRows(1);
        //表头行数,默认1
        importParams.setTitleRows(0);
        importParams.setSheetNum(1);
        File excelFile = new File(excelPath);
        ExcelImportResult<WsdlService> wsdlServiceResult = ExcelImportUtil.importExcelMore(new FileInputStream(excelFile), WsdlService.class, importParams);
        List<WsdlService> wsdlServiceList = wsdlServiceResult.getList();
        List<WsdlService> serviceCodeList = new ArrayList<>();
        WsdlService base = null;
        for (int q = 0 ; q <wsdlServiceList.size() ; q++ ){
            WsdlService wsdlService = wsdlServiceList.get(q);
            if(org.apache.commons.lang3.StringUtils.isBlank(wsdlService.getOperateName())){
                continue;
            }
            if(!org.apache.commons.lang3.StringUtils.isBlank(wsdlService.getName())){
                base = wsdlServiceList.get(q);
            }
            if(wsdlService.getOperateName().equals("END")){
                break;
            }
            importParams = new ImportParams();
            importParams.setSheetNum(q+2);
            importParams.setHeadRows(1);
            importParams.setTitleRows(0);
            ExcelImportResult<WsdlElement> wsdlElementResult = ExcelImportUtil.importExcelMore(new FileInputStream(excelFile), WsdlElement.class, importParams);
            List<WsdlElement> wsdlElementList = wsdlElementResult.getList();
            int reqStart = 0 ;
            int reqEnd = 0;
            int rspStart = 0 ;
            int rspEnd = 0;
            List<String> kv = new ArrayList<>();
            for (int i = 0 ; i< wsdlElementList.size() ; i++){
                WsdlElement wsdlElement = wsdlElementList.get(i);
                if("list_start".equals(wsdlElement.getType())||"level_label_start".equals(wsdlElement.getType())){
                    kv.add(wsdlElement.getName());
                }
                if("list_end".equals(wsdlElement.getType())||"level_label_end".equals(wsdlElement.getType())){
                    if(!kv.isEmpty()){
                        kv.remove(wsdlElement.getName());
                    }else {
                        throw new Exception("循环体配对出现问题");
                    }
                }
                if(!"1".equals(wsdlElement.getMinOccurs())){
                    wsdlElement.setMinOccurs("0");
                }
                if(!"list_start".equals(wsdlElement.getType())&&!"level_label_start".equals(wsdlElement.getType())){
                    wsdlElement.setMaxOccurs("1");
                }else {
                    if("level_label_start".equals(wsdlElement.getType())){
                        wsdlElement.setMaxOccurs("1");
                    }
                    if("list_start".equals(wsdlElement.getType())){
                        if(!"unbounded".equals(wsdlElement.getMaxOccurs())){
                            BigDecimal max = new BigDecimal(wsdlElement.getMaxOccurs());
                            BigDecimal yi = new BigDecimal(1);
                            if(max.compareTo(yi)<1){
                                throw new Exception("list_start 最大出现次数错误");
                            }
                        }
                    }
                }

                if(StringUtils.isBlank(wsdlElement.getName())){
                    continue;
                }
                if(wsdlElement.getName().equals("REQ_S")){
                    reqStart = i;
                }
                if(wsdlElement.getName().equals("REQ_E")){
                    reqEnd = i;
                }
                if(wsdlElement.getName().equals("RSP_S")){
                    rspStart = i;
                }
                if(wsdlElement.getName().equals("RSP_E")){
                    rspEnd = i;
                }

            }
            if(!kv.isEmpty()){
                throw new Exception("循环体配对出现问题");
            }
            List<WsdlElement> req = wsdlElementList.subList(reqStart+1,reqEnd);
            List<WsdlElement> rsp = wsdlElementList.subList(rspStart+1,rspEnd);
            System.out.println(req);
            System.out.println(rsp);

            Map<String,Object> map = new HashMap<>();
            map.put("serviceTargetNamespace","http://esb.lzccb.com/business/corevm/"+base.getName());
            map.put("wsdlLocation","http://esb.lzccb.com");
            map.put("name",base.getName());
            map.put("method",wsdlService.getOperateName());
            map.put("documentation",wsdlService.getOperateDesc());
            map.put("address",address);

            WsdlElement SourceId = new WsdlElement("SourceId","string");
            WsdlElement AuthCode = new WsdlElement("AuthCode","string");
            WsdlElement GoalId = new WsdlElement("GoalId","string");
            WsdlElement ServiceName = new WsdlElement("ServiceName","string");
            WsdlElement OperationName = new WsdlElement("OperationName","string");
            List<WsdlElement> head = new ArrayList<>();
            head.add(SourceId);
            head.add(AuthCode);
            head.add(GoalId);
            head.add(ServiceName);
            head.add(OperationName);
            map.put("head",head);
            map.put("request",req);
            map.put("response",rsp);
            if(!req.isEmpty()){
                map.put("requestData",1);
            }
            if(!rsp.isEmpty()){
                map.put("responseData",1);
            }
            if("api".equals(type)){
                if(wsdlService.getServiceCode().equals(serviceCode)){
                    File file = new File(saveFilePath+File.separator+"api");
                    if(!file.exists()){
                        file.mkdirs();
                    }
                    TemplateConfig.generate(saveFilePath+File.separator+"api"+File.separator+id+".wsdl",map, "wsdl.ftl");
                    serviceCodeList.add(wsdlService);
                    break;
                }
            }else {
                File file = new File(saveFilePath+File.separator+id);
                if(!file.exists()){
                    file.mkdirs();
                }
                TemplateConfig.generate(saveFilePath+File.separator+id+File.separator+wsdlService.getServiceCode()+".wsdl",map, "wsdl.ftl");
                serviceCodeList.add(wsdlService);
            }

        }
        return serviceCodeList;
    }
}
